/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.man.cs.afzal.tcm.terms_sentence_handler;

import ac.man.cs.afzal.tcm.nlp.File_Manager_Impl;
import ac.man.cs.afzal.tcm.nlp.Vector_Builder_Impl;
import ac.man.cs.afzal.tcm.sql_handler.SQL_Handler_Terms_Impl;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This file is used to extract heads from terms.
 * Terms are read from a database table, heads are extracted from them, and then
 * terms and extracted heads are entered into a database table.
 * 
 * @author Hammad
 * Last Modified Date: 16th Jan, 2013
 */
public class Term_Head_Extractor
{

    private List<String> heads, terms;
    private List<Integer> head_frequency;

    private String database_url = "database_url";
    private String username = "username";
    private String password = "password";

    private String NEW_DISEASE_TERMS = "NEW_DISEASE_TERMS";
    private String METAMAP_INPUT_FILE = "METAMAP_INPUT_FILE";

    private SQL_Handler_Terms_Impl sql_handler;
    private Vector_Builder_Impl vector_builder = new Vector_Builder_Impl();
    private File_Manager_Impl file_manager = new File_Manager_Impl();


    public Term_Head_Extractor()
    {
        heads = new ArrayList();
        terms = new ArrayList();
        head_frequency = new ArrayList();

        Properties props = new Properties();
        try
        {
            props.load(new FileInputStream("Term_Classification.properties"));
        }
        catch (IOException ex)
        {
            Logger.getLogger(Term_Head_Extractor.class.getName()).log(Level.SEVERE, null, ex);
        }

        database_url = props.getProperty(database_url);
        username = props.getProperty(username);
        password = props.getProperty(password);
        NEW_DISEASE_TERMS = props.getProperty(NEW_DISEASE_TERMS);
        METAMAP_INPUT_FILE = props.getProperty(METAMAP_INPUT_FILE);

    }

    public void extract_heads()
    {
        sql_handler = new SQL_Handler_Terms_Impl();
        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();
        sql_handler.reset_tables(new String[]{"terms_and_heads"});
        
        //terms = sql_handler.get_terms_from_result_views(NEW_DISEASE_TERMS);
        terms = sql_handler.get_terms_from_result_views("new_diseases_lex_obm_rel1");

        
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_lex_obm_rel1");

        terms = sql_handler.get_terms_from_result_views("new_diseases_lex_obm_rel2");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_lex_obm_rel2");

        terms = sql_handler.get_terms_from_result_views("new_diseases_lex_gad_rel1");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_lex_gad_rel1");

        terms = sql_handler.get_terms_from_result_views("new_diseases_lex_gad_rel2");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_lex_gad_rel2");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cn_gad_rel1");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cn_gad_rel1");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cn_gad_rel2");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cn_gad_rel2");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cn_gad_rel3");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cn_gad_rel3");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cv_gad_rel1");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cv_gad_rel1");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cv_gad_rel2");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cv_gad_rel2");

        terms = sql_handler.get_terms_from_result_views("new_diseases_cv_gad_rel3");
        for (String term : terms)
            get_heads_insert_in_db(term,"new_diseases_cv_gad_rel3");


        sql_handler.close_db();
    }

    public void prepare_new_terms_for_metamap()
    {
        sql_handler = new SQL_Handler_Terms_Impl();
        sql_handler.initialize_parameters(database_url, username, password);
        sql_handler.connect_db();

        terms = sql_handler.get_terms_from_result_views(NEW_DISEASE_TERMS);
        String file = vector_builder.list_to_string(terms, ",");
        file_manager.fileWriter(file,METAMAP_INPUT_FILE );
        sql_handler.close_db();
    }

    private void get_heads_insert_in_db(String term, String view_name)
    {
        
        String temp = get_view_code_name(view_name);
        
        if (term.contains(" "))
        {
            int index_last_space = term.lastIndexOf(" ");
            String head = term.substring(index_last_space + 1, term.length()).trim();
            // build_heads_frequencies_list is not called if values of heads are to be entered directly in database
            //build_heads_and_frequencies_list(head);
            insert_terms_and_heads_in_db(term, head,temp);
        }
        else
        {
            insert_terms_and_heads_in_db(term, term,temp);
//            build_heads_and_frequencies_list(term);
        }

    }

    private void build_heads_and_frequencies_list(String head)
    {
        if (heads.contains(head))
        {
            int head_index = heads.indexOf(head);
            Integer frequency_head = head_frequency.get(head_index);
            frequency_head++;
            head_frequency.set(head_index, frequency_head);
        }
        else
        {
            heads.add(head);
            head_frequency.add(1);
        }
    }

    private void insert_terms_and_heads_in_db(String term, String head, String view_code_name)
    {
        String[] doc_table1 =
        {
            "Term", "Head", "Result_View"
        };
        String[] doc_table_value1 =
        {
            term, head, view_code_name
        };
        sql_handler.insert_table("terms_and_heads", doc_table1, doc_table_value1);
    }

    private String get_view_code_name(String view_name)
    {
        if (view_name.equals("new_diseases_lex_gad_rel1"))
        {
            return "LEX_GAD_1";
        }
        else if (view_name.equals("new_diseases_lex_gad_rel2"))
        {
            return "LEX_GAD_2";
        }
        else if (view_name.equals("new_diseases_cn_gad_rel1"))
        {
            return "CN_GAD_1";
        }
        else if (view_name.equals("new_diseases_cn_gad_rel2"))
        {
            return "CN_GAD_2";
        }
        else if (view_name.equals("new_diseases_cn_gad_rel3"))
        {
            return "CN_GAD_3";
        }
        else if (view_name.equals("new_diseases_cv_gad_rel1"))
        {
            return "CV_GAD_1";
        }
        else if (view_name.equals("new_diseases_cv_gad_rel2"))
        {
            return "CV_GAD_2";
        }
        else if (view_name.equals("new_diseases_cv_gad_rel3"))
        {
            return "CV_GAD_3";
        }
        else if (view_name.equals("new_diseases_lex_obm_rel1"))
        {
            return "LEX_OBM_1";
        }
        else if (view_name.equals("new_diseases_lex_obm_rel2"))
        {
            return "LEX_OBM_2";
        }

        else
        {
            System.out.println("Invalid value of Database Result View");
            System.exit(0);
        }

        return "";
    }
}